home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 10 / FM Towns Free Software Collection 10.iso / ms_dos / tool / fwcp / src / getopt.c < prev    next >
Text File  |  1994-02-14  |  2KB  |  77 lines

  1. #include    <stdio.h>
  2. #include    <string.h>
  3. #include    <ctype.h>
  4.  
  5.     char    *optarg = "";
  6.     int    optind = 1;
  7. static  char    *optstr = NULL;
  8.  
  9. char    *progname(char *prog)
  10. {
  11.     char    *p;
  12.  
  13.     if ( (p = strrchr(prog, ':')) != NULL )
  14.     prog = p + 1;
  15.     if ( (p = strrchr(prog, '\\')) != NULL )
  16.     prog = p + 1;
  17.     if ( (p = strrchr(prog, '/')) != NULL )
  18.     prog = p + 1;
  19.     for ( p = prog ; *p != '\0' ; p++ )
  20.     *p = tolower(*p);
  21.     if ( (p = strrchr(prog, '.')) != NULL && strcmp(p, ".exe") == 0 )
  22.     *p = '\0';
  23.     return prog;
  24. }
  25. int    getopt(ac, av, opt)
  26. int    ac;
  27. char    *av[];
  28. char    *opt;
  29. {
  30.     int     n, c;
  31.     char    *p;
  32.     char    *s;
  33.  
  34.     for ( ; ; ) {
  35.     if ( optstr != NULL && *optstr != '\0' ) {
  36.         s = opt;
  37.         while ( *s != '\0' ) {
  38.         if ( *(s++) == *optstr ) {
  39.             c = *(optstr++);
  40.             if ( *s == ':' ) {
  41.             s++;
  42.  
  43.             if ( *optstr == '=' )
  44.                 optstr++;
  45.  
  46.             if ( *optstr != '\0' )
  47.                 optarg = optstr;
  48.             else if ( optind < ac && *av[optind] != '-' )
  49.                 optarg = av[optind++];
  50.             else {
  51.                 fprintf(stderr,
  52.                 "%s: option `-%c' requires an argument\n",
  53.                 progname(av[0]), c);
  54.                 optstr = NULL;
  55.                 return '\0';
  56.             }
  57.  
  58.             optstr = NULL;
  59.             }
  60.             return c;
  61.         }
  62.         if ( *s == ':' )
  63.             s++;
  64.         }
  65.  
  66.         fprintf(stderr, "%s: unrecognized option `-%c'\n",
  67.                 progname(av[0]), *optstr);
  68.         return *(optstr++);
  69.     }
  70.  
  71.     if ( optind >= ac || *av[optind] != '-' )
  72.         return EOF;
  73.  
  74.     optstr = av[optind++] + 1;
  75.     }
  76. }
  77.